package com.zzuli.hosp.controller.api;

import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import com.baomidou.mybatisplus.extension.api.R;
import com.zzuli.common.exception.YyghException;
import com.zzuli.common.helper.HttpRequestHelper;
import com.zzuli.common.result.Result;
import com.zzuli.common.result.ResultCodeEnum;
import com.zzuli.common.utils.MD5;
import com.zzuli.hosp.service.DepartmentService;
import com.zzuli.hosp.service.HospitalService;
import com.zzuli.hosp.service.HospitalSetService;
import com.zzuli.hosp.service.ScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 上传医院
     * @param request
     * @return
     */
    @ApiOperation(value = "上传医院接口")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hospKey = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey((String) paramMap.get("hoscode"));
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!hospKey.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String)paramMap.get("logoData");
        if(!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            paramMap.put("logoData", logoData);
        }
        hospitalService.save(paramMap);
        return Result.ok();
    }

    /**
     * 显示医院
     * @param request
     * @return
     */
    @ApiOperation(value = "显示医院接口")
    @PostMapping("hospital/show")
    public Result showHospital(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");

        String hospKey = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey((String) paramMap.get("hoscode"));
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!hospKey.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 上传科室
     * @param request
     * @return
     */
    @ApiOperation(value = "上传科室接口")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hospKey = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey((String) paramMap.get("hoscode"));
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!hospKey.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(paramMap);
        return Result.ok();
    }

    /**
     * 显示科室
     * @param request
     * @return
     */
    @ApiOperation(value = "显示科室接口")
    @PostMapping("department/list")
    public Result showDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");

        //获取当前页和每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        //签名验证
        String hospKey = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey((String) paramMap.get("hoscode"));
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!hospKey.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 删除科室
     * @param request
     * @return
     */
    @ApiOperation(value = "删除科室接口")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        //签名验证
        String hospKey = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey((String) paramMap.get("hoscode"));
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!hospKey.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.removeDepartment(hoscode, depcode);
        return Result.ok();
    }

    /**
     * 上传排班
     * @return
     */
    @ApiOperation(value = "上传排班接口")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        String hospKey = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey((String) paramMap.get("hoscode"));
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!hospKey.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();
    }

    /**
     * 显示排班
     * @param request
     * @return
     */
    @ApiOperation(value = "显示排班接口")
    @PostMapping("schedule/list")
    public Result showSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        //获取当前页和每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        //签名验证
        String hospKey = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey((String) paramMap.get("hoscode"));
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!hospKey.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 删除排班
     * @param request
     * @return
     */
    @ApiOperation(value = "删除排班接口")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        //签名验证
        String hospKey = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey((String) paramMap.get("hoscode"));
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!hospKey.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }
}
